package com.jvm123.dytt

import groovy.sql.Sql

/**
 *
 * @author yawn
 * @date 2021/1/19 星期二
 *
 */

// sql db
url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true"
sql = Sql.newInstance(url, "root", "root", "com.mysql.cj.jdbc.Driver")




// file
FileReader fr = new FileReader("dytt.com.txt")
BufferedReader br = new BufferedReader(fr)
String line
while ((line = br.readLine()) != null) {
    if ("" == line.trim()) {
        continue
    }
    def name = getName(line)
    def href = getHref(line)
    def type = getType(line)
    def country = getCountry(line)
    def time = getTime(line)
    def actors = getActors(line)

    if (name == null || name == "") {
        continue
    }

    def insertSql = 'INSERT INTO movie_dytt (name, href, type, country, time, actors) VALUES (?, ?, ?, ?, ?, ?)'
    def params = [name, href, type, country, time, actors]
    def keys = sql.executeInsert(insertSql, params)
    println keys
    println name
}

sql.close()

def getName(String line) {
    String[] s1s = line.split(">")
    if (s1s.length >= 1) {
        String[] s2s = s1s[1].split("</")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}

def getType(String line) {
    String[] s1s = line.split("td>")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("</")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}

def getHref(String line) {
    String[] s1s = line.split("\"")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("\"")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}

def getTime(String line) {
    String[] s1s = line.split("td>")
    if (s1s.length >= 6) {
        String[] s2s = s1s[5].split("</")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}

def getCountry(String line) {
    String[] s1s = line.split("td>")
    if (s1s.length >= 4) {
        String[] s2s = s1s[3].split("</")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}

def getActors(String line) {
    String[] s1s = line.split("nowrap;\">")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("</")
        return s2s.length >= 1 ? s2s[0] : ""
    }
}